package car_rental.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class DaoUtils {

	private static final EntityManagerFactory factory = Persistence.createEntityManagerFactory("car-rental");

	private DaoUtils() {

	}

	@Deprecated
	public static EntityManager newManager() {
		return factory.createEntityManager();
	}
	
	private static ThreadLocal<LocalThreadEntityManager> r = new ThreadLocal<LocalThreadEntityManager>();
	
	public static EntityManager getLocalManager() {
		LocalThreadEntityManager m = r.get();
		if ( (null == m) || (!m.isOpen()) ) {
			m = new LocalThreadEntityManager(newManager());
			r.set(m);
		} else {
			m.referenced();
		}
		return m;
	}
}
